package com.douma.tool.db;

import com.douma.tool.db.tables.TablesBaks;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description 数据表新建SQL生成器
 * @Author haoxijun
 * @Date 2019/12/9
 * @Time 17:29
 */
public class SqlGenerater {

    public static void main(String[] args) {

        Map map = TablesBaks.axg_mate_label();

        String tableName = (String) map.get("tableName");
        String tableNameDes = (String) map.get("tableNameDes");
        List<DbFieldDto> dtoList = (List<DbFieldDto>) map.get("dtoList");

        List<String> lines = generat(tableName, tableNameDes, dtoList);

        for (String line : lines) {
            System.out.println(line);
        }

    }

    public static List<String> generat(String tableName, String tableNameDes, List<DbFieldDto> dtoList) {
        List<String> lines = new ArrayList<String>();

        List<String> idxs = new ArrayList<String>();

        lines.add("CREATE TABLE `" + tableName + "` (");
        lines.add("\t`id` BIGINT (20) NOT NULL COMMENT '主键id',");
        for (DbFieldDto dto : dtoList) {
            String name = dto.getName();
            String type = dto.getType().getType();
            String length = dto.getType().getLength();
            String notNull = dto.getNotNull();
            if ("notNull".equals(notNull)) {
                notNull = "NOT NULL";
            } else {
                if ("NULL".equals(notNull) || "null".equals(notNull)) {
                    notNull = "DEFAULT " + notNull;
                } else {
                    notNull = "DEFAULT '" + notNull + "'";
                }
            }
            String comment = dto.getComment();
            if (DbTypeEnum.TEXT.getType().equals(type)) {
                lines.add("\t`" + name + "` " + type + " " + notNull + " COMMENT '" + comment + "',");
            } else {
                lines.add("\t`" + name + "` " + type + " (" + length + ") " + notNull + " COMMENT '" + comment + "',");
            }
            if (dto.isIdx()) {
                idxs.add(dto.getName());
            }
        }
        lines.add("\t`is_deleted` CHAR (1) NOT NULL DEFAULT 'N' COMMENT '是否删除',");
        lines.add("\t`creator` VARCHAR (32) NOT NULL DEFAULT 'SYSTEM' COMMENT '创建人',");
        lines.add("\t`gmt_created` datetime NOT NULL COMMENT '创建时间',");
        lines.add("\t`modifier` VARCHAR (32) NOT NULL DEFAULT 'SYSTEM' COMMENT '修改人',");
        lines.add("\t`gmt_modified` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',");
        String ukText = "";
        for (String idx : idxs) {
            ukText = ukText + "," + "`" + idx + "`";
        }
        if (idxs.size() == 0) {
            lines.add("\tPRIMARY KEY (`id`)");
            lines.add(") ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = '" + tableNameDes + "';");
        } else {
            lines.add("\tPRIMARY KEY (`id`),");
            lines.add("\tUNIQUE KEY `uk_" + tableName + "` (" + ukText + ")");
        }

        return lines;
    }
}
